package com.xhorse.lockplatform.controller;

import com.xhorse.lockplatform.annotation.RequestLimit;
import com.xhorse.lockplatform.comm.enums.ResultEnum;
import com.xhorse.lockplatform.comm.mvc.AbstractCallbackController;
import com.xhorse.lockplatform.comm.util.RequestUtils;
import com.xhorse.lockplatform.model.XhErrorLib;
import com.xhorse.lockplatform.model.XhFeedbackWithBLOBs;
import com.xhorse.lockplatform.model.XhUsingLog;
import com.xhorse.lockplatform.model.pojo.XhUsingUploadPojo;
import com.xhorse.lockplatform.service.FeedBackService;
import com.xhorse.lockplatform.service.UsingLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("FeedBack")
public class FeedBackController extends AbstractCallbackController {

    @Autowired
    private FeedBackService feedBackService;

    @Autowired
    private UsingLogService usingLogService;

    /**
     * 获取错误种类
     *
     * @param type
     * @return
     */
    @RequestMapping(value = "getErrorLib", method = RequestMethod.POST)
    @ResponseBody
    public void getErrorLib(HttpServletRequest request, HttpServletResponse response, byte type, byte languageId) throws Exception {
        List<XhErrorLib> result = feedBackService.getErrorLib(type, languageId);
        if (result.isEmpty()) {
            simpleFailed(response, ResultEnum.CRUD_RESULT_ERROR.getMsg());
            return;
        }
        simpleSuccess(response, result);
    }

    /**
     * 上传反馈信息
     *
     * @param feedback
     * @return
     */
    @RequestLimit(count = 3, time = 10000)
    @RequestMapping(value = "uploadFeedBack", method = RequestMethod.POST)
    @ResponseBody
    public void uploadFeedBack(HttpServletRequest request, HttpServletResponse response, XhFeedbackWithBLOBs feedback)
            throws Exception {
        boolean result = feedBackService.uploadFeedBack(feedback);
        if (!result) {
            simpleFailed(response, ResultEnum.CRUD_REPEATED_COMMIT.getMsg());
            return;
        }
        simpleSuccess(response);
    }


    /**
     * 上传使用日志
     *
     * @param xhUsingUploadPojo 日志对象
     */
    @RequestMapping(value = "uploadUsingLog", method = RequestMethod.POST)
    @RequestLimit(count = 10, time = 60000)
    @ResponseBody
    public void uploadUsinLog(HttpServletRequest request, HttpServletResponse response,
                              @RequestBody XhUsingUploadPojo xhUsingUploadPojo) throws Exception {
        List<XhUsingLog> list = xhUsingUploadPojo.getList();
        for (XhUsingLog xhUsingLog : list) {
            String ip = RequestUtils.getRemoteIp(request);
            xhUsingLog.setIp(ip);
            if (xhUsingLog.getIsSave() == 0) {
                xhUsingLog.setCreateAt(null);
            }
            usingLogService.uploadUsinLog(xhUsingLog);
        }
        simpleSuccess(response);
    }
}
